package org.gdstash.character;

import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;
import org.gdstash.util.FileVersionException;
import org.gdstash.util.GDMsgFormatter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class GDCharRespawnList {
    private static final byte[] RESPAWN_DEVILS_CROSSING;
    private static final byte[] RESPAWN_HOMESTEAD;
    private static final byte[] RESPAWN_FORT_IKON;
    public static final GDCharUID UID_SPAWN_DEVILS_CROSSING;
    public static final GDCharUID UID_SPAWN_HOMESTEAD;
    public static final GDCharUID UID_SPAWN_FORT_IKON;
    private static final int VERSION = 1;
    private static final int BLOCK = 5;
    private int version;
    private ArrayList<List<GDCharUID>> uidLists;
    private GDCharUID[] spawns;
    private boolean changed;

    public GDCharRespawnList() {
        final int size = 3;
        this.uidLists = new ArrayList<List<GDCharUID>>(size);
        for (int i = 0; i < size; ++i) {
            this.uidLists.add(new LinkedList<GDCharUID>());
        }
        this.spawns = new GDCharUID[3];
    }

    public GDCharUID getRespawnPoint(final int difficulty) {
        final GDCharUID spawn = null;
        if (difficulty < 0 || difficulty > 2) {
            return spawn;
        }
        return this.spawns[difficulty];
    }

    public boolean hasChanged() {
        return this.changed;
    }

    public void setRespawnPoint(final int difficulty, final GDCharUID spawn) {
        if (difficulty < 0 || difficulty > 2) {
            return;
        }
        this.spawns[difficulty] = spawn;
        this.changed = true;
    }

    public void read() throws IOException {
        int val = 0;
        final GDReader.Block block = new GDReader.Block();
        val = GDReader.readBlockStart(block);
        if (val != 5) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        this.version = GDReader.readEncInt(true);
        if (this.version != 1) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        for (int i = 0; i < this.uidLists.size(); ++i) {
            GDChar.readUIDList(this.uidLists.get(i));
        }
        for (int i = 0; i < this.spawns.length; ++i) {
            final GDCharUID uid = new GDCharUID();
            uid.read();
            this.spawns[i] = uid;
        }
        GDReader.readBlockEnd(block);
        this.changed = false;
    }

    public void write() throws IOException {
        final GDReader.Block block = new GDReader.Block();
        GDWriter.writeBlockStart(block, 5);
        GDWriter.writeInt(this.version);
        for (int i = 0; i < this.uidLists.size(); ++i) {
            GDChar.writeUIDList(this.uidLists.get(i));
        }
        for (int i = 0; i < this.spawns.length; ++i) {
            this.spawns[i].write();
        }
        GDWriter.writeBlockEnd(block);
        this.changed = false;
    }

    public int getByteSize() {
        int size = 0;
        size += 4;
        size += 4;
        size += 4;
        for (int i = 0; i < this.uidLists.size(); ++i) {
            size += 4;
            size += this.uidLists.get(i).size() * GDCharUID.getByteSize();
        }
        size += this.spawns.length * GDCharUID.getByteSize();
        size += 4;
        return size;
    }

    public void print() {
        for (int i = 0; i < this.uidLists.size(); ++i) {
            System.out.println("Index : " + Integer.toString(i));
            final String s = "";
            for (final GDCharUID uid : this.uidLists.get(i)) {
                uid.println();
            }
            System.out.println();
        }
        for (int i = 0; i < this.spawns.length; ++i) {
            System.out.println("Spawn : " + Integer.toString(i));
            this.spawns[i].println();
        }
    }

    static {
        RESPAWN_DEVILS_CROSSING = new byte[]{110, -113, 45, 112, -107, 79, 56, 103, 93, 21, 75, -98, 88, 4, 108, 6};
        RESPAWN_HOMESTEAD = new byte[]{-121, 64, 3, 63, -43, 76, -62, 58, -20, 76, -104, -69, 28, -65, -111, -115};
        RESPAWN_FORT_IKON = new byte[]{-128, 79, -90, 79, 36, 79, 46, -40, -91, -68, 58, -87, 124, 114, 33, -26};
        UID_SPAWN_DEVILS_CROSSING = new GDCharUID(GDCharRespawnList.RESPAWN_DEVILS_CROSSING);
        UID_SPAWN_HOMESTEAD = new GDCharUID(GDCharRespawnList.RESPAWN_HOMESTEAD);
        UID_SPAWN_FORT_IKON = new GDCharUID(GDCharRespawnList.RESPAWN_FORT_IKON);
    }
}
